package com.zklh.leetcode.interviewTop150;

import java.util.Arrays;

/**
 * 轮转数组
 *
 * @Author tongnan
 * @Date 2024/4/28
 * @Version 1.0
 */
public class Solution189 {
    public void rotate(int[] nums, int k) {
        reverse(nums, 0, nums.length - 1);
        k = k % nums.length;

        reverse(nums, 0, k-1);
        reverse(nums, k, nums.length-1);
    }

    private void reverse(int[] nums, int start, int end) {
        int left = start;
        int right = end;
        int temp;

        while (left < right) {
            temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }
    }
    public static void main(String[] args) {
        Solution189 solution189 = new Solution189();
        int[] nums = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
        int k = 10;
        solution189.rotate(nums, k);
        System.out.println(Arrays.toString(nums));

//        1,2,3,4,5,6,7,8,9,10,11,            12,13,14,15,16,17,18,19,20,21,22,   23,24,25,26,27
//        12,13,14,15,16,17,18,19,20,21,22,   1,2,3,4,5,6,7,8,9,10,11,            23,24,25,26,27
//        23,24,25,26,27,17,18,19,20,21,22   1,2,3,4,5,6,7,8,9,10,11,            12,13,14,15,16
//        17,18,19,20,21,22,24,25,26,27,23   1,2,3,4,5,6,7,8,9,10,11,            12,13,14,15,16
    }
}